package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.BrandMapper;
import com.cskaoyan.model.Brand;
import com.cskaoyan.model.BrandExample;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.bo.BrandListBO;
import com.cskaoyan.model.vo.BaseRespVO;
import com.cskaoyan.model.vo.BrandDataVO;
import com.cskaoyan.model.vo.WxBrandDataVO;
import com.cskaoyan.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author ：王李轶群
 * @description：TODO
 * @date ：5/5/22 8:56 PM
 */
@Service
@Transactional
public class BrandServiceImpl implements BrandService {

    @Autowired
    BrandMapper brandMapper;

    @Override
    public BaseRespVO<BrandDataVO> getBrandList(BrandListBO brandListBO) {
        PageHelper.startPage(brandListBO.getPage(), brandListBO.getLimit());
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        if (brandListBO.getId() != null) {
            criteria.andIdEqualTo(brandListBO.getId());
        }
        if (brandListBO.getName() != null && brandListBO.getName() != "") {
            criteria.andNameLike("%" + brandListBO.getName() + "%");
        }
        criteria.andDeletedEqualTo(false);
        brandExample.setOrderByClause(brandListBO.getSort() + " " + brandListBO.getOrder());
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        PageInfo<Brand> brandPageInfo = new PageInfo<>(brands);
        long total = brandPageInfo.getTotal();
        BrandDataVO brandDataVO = new BrandDataVO();
        brandDataVO.setLimit(brandListBO.getLimit());
        brandDataVO.setPage(brandListBO.getPage());
        brandDataVO.setPages(brandPageInfo.getPages());
        brandDataVO.setList(brands);
        brandDataVO.setTotal((int) total);
        BaseRespVO<BrandDataVO> brandDataVOBaseRespVO = new BaseRespVO<>();
        brandDataVOBaseRespVO.setData(brandDataVO);
        brandDataVOBaseRespVO.setErrno(0);
        brandDataVOBaseRespVO.setErrmsg("成功");
        return brandDataVOBaseRespVO;
    }

    @Override
    public BaseRespVO<Brand> createBrand(Brand brand) {
        Date date = new Date(System.currentTimeMillis());
        brand.setAddTime(date);
        brand.setUpdateTime(date);
        brandMapper.insertAndGetId(brand);
        BaseRespVO<Brand> brandBaseRespVO = new BaseRespVO<>();
        brandBaseRespVO.setErrmsg("成功");
        brandBaseRespVO.setErrno(0);
        brandBaseRespVO.setData(brand);
        return brandBaseRespVO;
    }

    @Override
    public void deleteBrand(Brand brand) {
        brand.setDeleted(true);
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public BaseRespVO<Brand> updateBrand(Brand brand) {
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        criteria.andIdEqualTo(brand.getId());
        brand.setUpdateTime(new Date(System.currentTimeMillis()));
        brandMapper.updateByExampleSelective(brand, brandExample);
        BaseRespVO<Brand> brandBaseRespVO = new BaseRespVO<>();
        brandBaseRespVO.setErrno(0);
        brandBaseRespVO.setErrmsg("成功");
        brandBaseRespVO.setData(brand);
        return brandBaseRespVO;
    }

}
